package com.hpe.wx.performance.redis.task;

import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hpe.wx.performance.redis.service.PerFormanceService;

import redis.clients.jedis.Tuple;

@Component
public class GetJMXData {
	private static Logger logger = Logger.getLogger(GetJMXData.class.getSimpleName());
	@Autowired
	private PerFormanceService service;
	
	//@Scheduled(cron="0/5 * *  * * ? ")
	//@Scheduled(fixedRate = 1000 * 5)
    public void taskCycle(){
        System.out.println("=========================================a");
    }

	/**
	 * heartbeat, it will execute per 30 seconds, to get data from jmx and store into redis
	 * if there is not any jmx or just for test, comment it  
	 */
	@Scheduled(fixedRate = 1000 * 15)
	public void getJMXData() {
		Set<Tuple> urls = service.getAllJMXURLs();
		for(Tuple tup : urls){//如果下面的程序中出现异常，将影响 
			String elememt = tup.getElement();
			String [] jmxUrlandName = elememt.split("#");
			String jmxURL = jmxUrlandName[0];
			logger.info("The jmx for get info from remote machine is: " + jmxURL);
			service.addDate2Redis(jmxURL);
		}
	}
	
}
